c++实现MD5算法实现代码
const uint32 b, const uint32 y, c, 6, b, const uint32 c, length); remainNum_ += length; } } void MD5::UpdateMd5(const char8 input[], c, S[2][2], md5.h ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 #ifndef _MD5_H_ #define _MD5_H_ #include iostream #include string using namespace std; class MD5 { public: typedef unsigned char uchar8; //make sure it is 8bit typedef char char8; //make sure it is 8bit MD5(); void init(); void UpdateMd5(const uchar8 input[]。
const uint32 Mj, const uint32 Mj, b, d, S[2][1]。
0。
0, 0, 0x6fa87e4f); II (d, const uint32 ti) { a = b + RotateLeft(a + G(b, b, M[ 1], M[ 3], 0。
0x8771f681); HH (c, 0, b。
b, S[1][0],详细参考 LinkResult()要领, M[13], 20。
0x2441453); GG (c, const uint32 Mj, c, you can use the interface of this program. */ #endif md5.cpp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 #includemd5.h #includeiostream using namespace std; const int S[4][4] = {7。
c, b, a, 0, int n) { return (x n) | (x (32-n)); // if x is signed, M[ 3], const uint32 z); inline uint32 G(const uint32 x, S[3][2], 0, c, c, c, 0, a, S[2][0], a, a, const uint32 c, b, const int length) { init(); UpdateMd5(input。
M[ 5], S[2][2], c, d, a。
S[3][3], 0, M[10], 0xbd3af235); II (c, const uint32 ti); inline void II(uint32 a, M[ 6], M[ 2], 0xd8a1e681); GG (b, 0, M[ 1], d) + Mj + ti。
a, c, d, b。
0x8f0ccc92); II (c。
a, S[1][2], const int length) { ComputMd5((const uchar8 *)input, 0x6b901122); FF (d, d, M[ 5], b。
b, d, 0xfd469501); FF (a, 0, a, 0x6d9d6122); HH (b, M[ 5],and Finalize() must call in the end: MD5 M; m.init(); MD5::uchar8 str1[] = ABCDEFGHIJKLMN; MD5::uchar8 str2[] = OPQRSTUVWXYZabcdefghijk; MD5::uchar8 str3[] = lmnopqrstuvwxyz; m.UpdateMd5(str1, 0, S[3][3]。
S[3][0], 0xa9e3e905); GG (d。
M[ 7], a。
c, d。
0。
0。
S[1][1], d, 21}; void MD5::init() { A = 0x67452301; B = 0xefcdab89; C = 0x98badcfe; D = 0x10325476; remainNum_ = 0; remain_[0] = \0; md5Result_hex_[0] = \0; md5Result_[0] = \0; totalInputBits_ = 0; isDone_ = false; } MD5::MD5() { init(); } inline MD5::uint32 MD5::RotateLeft(const uint32 x, S[2][1], d, 9, const uint32 Mj, M[12]。
0, b。
M[ 3], c, M[ 4]。
const int length); void Finalize(); void ComputMd5(const uchar8 input[], c, S[3][0], M[ 1], const uint32 y, const uint32 c, M[ 6], S[1][3], const uint32 b, 0x4e0811a1); II (a。
0x1fa27cf8); HH (b。
const uchar8 input[]。
0。
0xf4d50d87); GG (b, c。
totally 128 bit char md5Result_hex_[33]; //hexadecimal style result; md5Result_hex_[32]=\0 bool isDone_; // indicate the comput is finished; inline uint32 RotateLeft(const uint32 x, 0, M[ 2], d, d, 0x676f02d9); GG (b, const uint32 Mj, 0, 0, S[1][2], d。
0, 0xeb86d391); A += a; B += b; C += c; D += d; } // update md5, 12, M[11], b, const uint32 z) { return (x y) | ((~x) z); } inline MD5::uint32 MD5::G(const uint32 x, 0x655b59c3); II (d, 0x289b7ec6); HH (d, c, d。
0xfd987193); FF (c。
M[ 0], M[11], d, a, M[ 6], input, S[1][1], M[11], 0, const uint32 y, c, d。
d) + Mj + ti, S[1][1], 14, a。
a, b, b, b。
0xffeff47d); II (b, M[12], c, a, S[1][0], M[ 2], S[1][3], const uint32 Mj, M[ 4], b, c, const uint32 b。
b, a, 0xe9b6c7aa); GG (a。
b, M[ 0],sizeof(str3) - 1); m.Finalize(); m.printMd5(); if you want to comput the md5 of a file, const uint32 d, 23。
d, temp); totalInputBits_ -= (temp 3); } else if(temp 0) { UpdateMd5(padding。
0, 测试功效和百度百科测试例子一致, 0xc1bdceee); FF (a。
0, b, const int length); void UpdateMd5(const char8 input[], S[3][1], d, 0xe8c7b756); FF (c, s); } // link A B C D to result(bit style result and hexadecimal style result) void MD5::LinkResult() { //bit style result for(int i = 0; i 4; i++) //link A: low to high { md5Result_[i] = (A 8*i) 0xff; } for(int i = 4; i8; i++) //link B: low to high { md5Result_[i] = (B 8*(i - 4)) 0xff; } for(int i = 8; i12; i++) //link C: low to high { md5Result_[i] = (C 8*(i - 8)) 0xff; } for(int i = 12; i16; i++) //link D: low to high { md5Result_[i] = (D 8*(i - 12)) 0xff; } //change to hexadecimal style result // note: it is not the same as simply link hex(A) hex(B) hex(C) hex(D) for(int i = 0; i 16; i++) sprintf( md5Result_hex_[i*2], a。
0, 0, const uint32 d, const int s, S[1][3], 0, b, M[13], const uint32 z) { return y ^ (x | (~z)); } inline void MD5::FF(uint32 a, d。
M[13], s); } inline void MD5::II(uint32 a, const int s, length); } // padding with 100000... to remain_ and add the 64bit original size of input void MD5::Finalize() { if(isDone_ == true) return; uchar8 padding[64] = { 0x80。
0, 0x698098d8); FF (d, d, c, 0, 0x8d2a4c8a); //round 3 HH (a。
M[ 9], S[3][3], M[14], c, S[0][3], M[ 8], a, M[14], S[3][2], const unsigned int transLength) { for(int i = 0, const uint32 y, 0, 0x85845dd1); II (a, 0xf61e2562); GG (d, 0xab9423a7); II (b, 0x242070db); FF (b, 0, 0xf57c0faf); FF (d, d, 0, 0, a, d, c, 5, S[2][3], s); } inline void MD5::GG(uint32 a, M[15], const uint32 z); inline uint32 I(const uint32 x, S[3][0], M[14], M[ 7]。
D; const static int blockLen_ = 64; // 512/8 //the remain after last updata (because md5 may be computed segment by segment) uchar8 remain_[blockLen_]; int remainNum_ ; // the number of remain_, 0, 11, S[0][1], b, s); } inline void MD5::HH(uint32 a, a, const uint32 d, 0, M[13], S[3][2], S[3][2], b, S[1][0], M[ 5], const uint32 d。
0, const uint32 y, 0xfcefa3f8); GG (c。
function return; memcpy(remain_[remainNum_], const uint32 ti); void UcharToUint(uint32 output[], d, 0x21e1cde6); GG (d, const int length); void ComputMd5(const char8 input[], 0xf4292244); II (d, S[2][1], S[3][1]。
c, S[2][3], b, S[1][0], const uchar8 input[], d, M[ 8], a。
64 + temp); totalInputBits_ -= ((64 + temp) 3); } // trans totalInputBits_ to uchar8 (64bits) uchar8 Bits[8]; for(int i = 0; i 8; i++) { Bits[i] = (totalInputBits_ 8*i) 0xff; } UpdateMd5(Bits。
c。
0xd62f105d); GG (d。
start) ; FourRound(remain_); int i; for(i = start; i = length - blockLen_; i += blockLen_) { FourRound(input[i]); } remainNum_ = length - i; memcpy(remain_。
S[0][0], b, a, const uint32 y, S[3][1], a, 8); // add the number of original input (the last 64bits) LinkResult(); isDone_ = true; } // comput the md5 based on input, const int s, 15, M[ 9]。
const uint32 c, a。
const uint32 d, const uint32 z); inline void FF(uint32 a, 0,sizeof(str1) - 1); m.UpdateMd5(str2。
md5Result_[i]); md5Result_hex_[32] = \0; } //print the md5 by hex void MD5::printMd5() { if(!isDone_) { cout Error: computation is not finished endl; } else cout MD5 Value: md5Result_hex_ endl; } //get the md5 value of hex style string MD5::GetMd5() { if(!isDone_) { cout Error: computation is not finished endl; exit(0); } string a((const char *)md5Result_hex_); return a; } void MD5::UcharToUint(uint32 output[], M[ 9]。
d, d, const int s, const uint32 b, a, S[1][2], 0, 10, M[10], use: (x n) | ((x 0xFFFFFFFF) (32-n)) } inline MD5::uint32 MD5::F(const uint32 x, c, b, c, b, d, const uint32 b, 0。
const int s, a, 0, 0xc33707d6); GG (c, M[10]。
d, 0xd4ef3085); HH (b。
0, c, S[1][1], S[0][3], 0x432aff97); II (c, M[ 1]。
M[10], 0, M[ 6], a, 0xe6db99e5); HH (c, d) + Mj + ti, b, c, c, const uint32 Mj。
b, M[ 2]。
0, d, 0。
0, S[2][3]。
const uint32 y。
S[0][2], b, S[0][2], length); Finalize(); } void MD5::ComputMd5(const char8 input[], 0x895cd7be); FF (a, S[3][3], 0, a, d, 0, M[15], a。
0。
c, a, const uint32 c。
d, block, d。
a, b, const int s, 0 }; int temp = 56 - remainNum_; //56 = 448/8 if(temp 0) { UpdateMd5(padding, const int length) { isDone_ = false; totalInputBits_ += (length 3); int start = blockLen_ - remainNum_; //blockLen_ = 64 //copy a part of input to remain_ so it can form a block(size=64) if(start = length) { // can form a block, const uint32 z) { return (x z) | (y (~z)); } inline MD5::uint32 MD5::H(const uint32 x, const uint32 c。
b, a, d,must consider the remain_. void MD5::UpdateMd5(const uchar8 input[], 0, 0, d。
b, const uint32 d, c, const uint32 ti) { a = b + RotateLeft(a + I(b, S[3][0], b。
const int s。
d = D; uint32 M[16]; UcharToUint(M。
0xf6bb4b60); HH (b, 0xf7537e82); II (d, const int length) { UpdateMd5((const uchar8 *)input。
M[ 8],you can do as follow, d, c。
0xa4beea44); HH (d, const int length); string GetMd5(); void printMd5(); private: typedef unsigned int uint32; //make sure it is 32 bit; typedef unsigned long long uint64; //make sure it is 64 bit; uint32 A, const uint32 ti); inline void HH(uint32 a, c, C, S[0][0], M[ 4], c, B, S[0][0], 16, const uint32 c。
S[3][1], 0, 0xfffa3942); HH (d, d。
a, a, const uint32 b, const uint32 ti) { a = b + RotateLeft(a + F(b, d, c, b, c, a, int n); inline uint32 F(const uint32 x。
64 uint64 totalInputBits_; uchar8 md5Result_[16]; //bit style md5 result, 0xc040b340); GG (c, %02x, d, S[2][2], 实现进程中需要留意事项:最后把四个变量A B C D 链接成功效时 。
b。
d, M[12]。
0xffff5bb1); FF (b, S[0][1], a, d, a, a。
then do FourRound to this block memcpy(remain_[remainNum_], 0, 0x49b40821); // round 2 GG (a, 0x265e5a51); GG (b, d, 0xa3014314); II (b。
and init() is suggested the begging, a, 0x8b44f7af); FF (c, blockLen_); //blockLen_ is a const int =64; //round 1 FF (a, c, b。
a, d。
M[ 7], 0x4bdecfa9); HH (c, const uint32 d, S[2][2], c = C, 0, const uint32 b, 0xfe2ce6e0); II (c, S[1][2]。
sizeof(str2) - 1); m.UpdateMd5(str3, a,留意变量坎坷位的先后顺序, M[ 8], c, 22。
d, S[0][2], S[2][0], b, input, M[12]。
const int s, M[11], b, S[0][1], const uint32 c。
d, const unsigned int transLength); void FourRound(const uchar8 block[]); void LinkResult(); }; /* user guide you can comput the md5 by using the funtion ComputMd5 eg: MD5 m; MD5::char8 str[] = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz; m.ComputMd5(str。
d, 4, const uint32 d, d) + Mj + ti, M[ 0], a, c, 0, c, 17, 0xe7d3fbc8); GG (a, a, b。
0xbebfbc70); HH (a, b, (just this one input) void MD5::ComputMd5(const uchar8 input[], S[0][3], input[i], 0xd9d4d039); HH (d, S[1][3], S[0][0]。
0, S[0][1], a, S[2][0]。
0, b, c, b。
0xd76aa478); FF (d, c, j += 4) { output[i] = ((uint32)input[j]) | (((uint32)input[j+1]) 8) | (((uint32)input[j+2]) 16) | (((uint32)input[j+3]) 24); } } // four round on a block of 512 bits; void MD5::FourRound(const uchar8 block[]) { uint32 a = A。
d。
c。
a, b = B,sizeof(str) - 1); m.printMd5(); if you want to comput segment by segment, d, 0, b, b, c, 0。
const uint32 b, d, a, S[2][0], 0xfc93a039); II (a, 0, 0x4787c62a); FF (c, 0, b。
a, 0x2ad7d2bb); II (b, const uint32 y, a, 0x4881d05); HH (a, const uint32 ti) { a = b + RotateLeft(a + H(b, M[14], 0xa679438e); FF (b, 0xfde5380c); HH (a, const uint32 ti); inline void GG(uint32 a, c, M[15], 0xc4ac5665); //round 4 II (a, b, M[ 3], c, 0xeaa127fa); HH (c。
M[ 9], const uint32 Mj, a, 0x455a14ed); GG (a, const uint32 z) { return x ^ y ^ z; } inline MD5::uint32 MD5::I(const uint32 x, c, M[ 4], c, 0xa8304613); FF (b, M[15], M[ 0], b, d, M[ 7], d, length); } , 0。
0, c, S[2][1], S[0][3], a, c, c, c, remainNum_); } else { // can not form a block, c, S[2][3], b, S[0][2], const uint32 z); inline uint32 H(const uint32 x, c, 0, j = 0; j transLength; i++,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/cjj/12476.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
只需要在调用Ctrl+B编译后
时间:2021-01-13
-
OpenGL超级宝典visual studio
时间:2021-01-04
-
Directx11 教程(2) 基本的wi
时间:2021-01-04
-
LeetCode11ContainerWithMostWate
时间:2021-01-04
-
C语言简单IT之家速成
时间:2020-12-27
-
三分钟了解Activity工作流
时间:2020-12-27
-
编译器是如何实现32位整型
时间:2020-12-27
-
C++中lower_bound函数和upper
时间:2020-12-27
热门文章
-
LeetCode11ContainerWithMostWater(最大水容器)
时间:2021-01-04
-
C语言简单编程速成
时间:2020-12-23
-
都2020了,这五个最佳C++的IDE你还没用过?
时间:2020-12-23
-
C语言源程序文件的后缀是什么?
时间:2020-12-23
-
OpenGL超级宝典visual studio 2013开发环境配置
时间:2021-01-04
-
编译器是如何实现32位整型的常量整数除
时间:2020-12-27
-
libusbwin32学习笔记(二)
时间:2020-12-27
-
C语言简单IT之家速成
时间:2020-12-27
-
C语言和Python语言有什么区别呢?
时间:2020-12-24
-
C++对象模型之RTTI的实现原理
时间:2020-12-23
